// pages/register/register.js
var app = getApp(); //获取应用实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company:[],
    company2:[],
    companyIndex:0,
    name:'',
    phone: '',
    password: ''
  },
  nameInput: function (e) {
    this.setData({ name: e.detail.value })
  },
  phoneInput: function (e) {
    this.setData({ phone: e.detail.value })
  },
  passwordInput: function (e) {
    this.setData({ password: e.detail.value })
  },
  bindCompanyChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      companyIndex: e.detail.value
    })
  },
  register:function(e){
    console.log(this.data.phone)
    if (!/^1\d{10}$/.test(this.data.phone)) {
      app.tip.toast('手机号码格式不正确')
      return false;
    }
    if (this.data.password == '') {
      app.tip.toast('密码不能为空')
      return false;
    }
    if (this.data.name == '') {
      app.tip.toast('真实姓名不能为空')
      return false;
    }
    var that = this;
    var param = {};
    param.telephone = this.data.phone;
    param.password = this.data.password;
    param.username = this.data.name;
    param.companyId = this.data.company2[this.data.companyIndex].companyId;
    app.requestAPI.register(param)
      .then(d => {
        console.log(d)
        app.tip.toast(d.msg)
        wx.navigateBack({})
      }).catch(e => {
        console.log(e)
        app.tip.toast(e.errMsg)
        app.tip.log('getIndex请求异常')
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.requestAPI.getCompanys({})
      .then(d => {
        if (d.success){
          var company = [];
          for (var item of d.data){
            company.push(item.companyName);
          }
          that.setData({ company, company2:d.data })
        }
        
      }).catch(e => {
        console.log(e)
        app.tip.toast(e.errMsg)
        app.tip.log('getIndex请求异常')
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})